import shutil
import datetime
import os
import tarfile

def backup_files(source_path, target_path):
    ## 创建备份文件名 包含当前时间戳
    timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
    backup_filename = f"{target_path}/backup_{timestamp}.tar.gz"

    ## 打包成 tar.gz 格式
    with tarfile.open(backup_filename,"w:gz") as tar:
        ## 仅添加文件夹内容 不包括路径
        tar.add(source_path,arcname=os.path.basename(source_path))
    print(f"备份已完成：{backup_filename}")

##备份
backup_files("","")